package com.ihcil.forecast.demo.listenser;

import lombok.SneakyThrows;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * Create by @baseException on 2022/11/3 22:02
 */

@Component
public class FileMonitor{
    private FileAlterationMonitor monitor = new FileAlterationMonitor(1000);

    /**
     * 给文件添加监听
     *
     * @param path 文件路径
     * @param listener 文件监听器
     */
    public void monitor(String[] path, FileAlterationListener listener) {
        for (int i = 0; i < path.length; i++) {
            FileAlterationObserver observer = new FileAlterationObserver(new File(path[i]));
            monitor.addObserver(observer);
            observer.addListener(listener);
        }

    }

    public void stop() throws Exception {
        monitor.stop();
    }

    public void start() throws Exception {
        monitor.start();
    }

}

